import Vue from "vue"
let prototype = Vue.prototype;

export default {
    stateText(listType, orderType, state) {
        if (listType == 't1') {
            switch (state) {
                case 0: return ['待支付', '为保证师傅按时上门，建议尽快选择师傅并完成支付'];
                case 1: return ['待师傅接单'];
                case 2: return ['服务中'];
                case 4: return ['待支付尾款'];
                case 5: return ['服务已完成，待确认'];
                case 6: return ['订单已完成'];
                case 7: return ['退款申请审核中'];
                case 8: return ['已退款'];
                case 9: return ['退款未通过'];
            }
        } else if (listType == 't2') {
            switch (state) {
                case 0:
                    if (orderType == 2) {
                        return ['待选择师傅'];
                    }
                    return ['待师傅接单'];
                case 1:
                    if (orderType == 2) {
                        return ['已选择师傅，待支付'];
                    }
                    return ['师傅已接单，待支付'];
                case 2: return ['服务中'];
                case 4: return ['待支付尾款'];
                case 5: return ['服务已完成，待确认'];
                case 6: return ['订单已完成'];
                case 7: return ['退款申请审核中'];
                case 8: return ['已退款'];
                case 9: return ['退款未通过'];
            }
        } else if (listType == 't3') {
            switch (state) {
                case 0: return ['待支付'];
                case 1: return ['待接单'];
                case 2: return ['进行中'];
                case 3: return ['已完成'];
            }
        }

        return ['未知', '#000000'];
    },
    /**
     * 下单
     */
    createOrder(order) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.create.t1",
                data: order,
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 支付
     */
    pay(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    uni.requestPayment({
                        ...response.data,
                        provider: "wxpay",
                        success() {
                            resolve();
                        },
                        fail() {
                            reject();
                        },
                    });
                }
            }).catch(e => { });
        });
    },
    /**
     * 订单列表
     */
    orderList(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    let list = [];
                    if (options.listType == 't1' || options.listType == 't2') {
                        response.data.forEach((item) => {
                            // 基础数据
                            let order = {
                                listType: options.listType,
                                id: item.id,
                                orderId: item.order,
                                cate: item.cate_name,
                                content: item.desc,
                                createTime: item.time,
                                serviceTime: item.times,
                                orderType: item.types,
                                price: item.money,
                                state: item.status,
                                rejectDesc: item.backdesc,
                                needAppraise: item.message <= 0,
                                workerList: [],
                            };
                            // 尾款
                            if (options.listType == 't1' || options.listType == 't2') {
                                order.retainageMoney = item.moneyb;
                            }
                            // 师傅
                            if (prototype.$utils.isType(item.shifu, "object")) {
                                order.worker = {
                                    id: item.shifu.id,
                                    name: item.shifu.name,
                                    avatar: item.shifu.avatar,
                                    type: item.shifu.status,
                                    times: item.shifu.shifu_date.count,
                                    favorableRate: item.shifu.shifu_date.good,
                                    grade: item.shifu.shifu_date.score,
                                    price: item.money,
                                }
                            }
                            // 师傅列表
                            if (options.listType == 't2' && item.shifus.length > 0) {
                                item.shifus.forEach(worker => {
                                    order.workerList.push({
                                        id: worker.id,
                                        name: worker.name,
                                        avatar: worker.avatar,
                                        price: worker.money,
                                    })
                                });
                            }
                            list.push(order);
                        });
                    } else if (options.listType == 't3') {
                        response.data.forEach((item) => {
                            let order = {
                                listType: options.listType,
                                id: item.id,
                                orderId: item.order,
                                cate: "货运订单",
                                content: item.desc,
                                createTime: item.times,
                                serviceTime: item.starttime,
                                orderType: item.types,
                                price: item.money,
                                state: item.status,
                                needAppraise: false,
                                worker: {},
                            };
                            // 师傅
                            if (prototype.$utils.isType(item.shifu, "object")) {
                                order.worker = {
                                    id: item.shifu.id,
                                    name: item.shifu.name,
                                    avatar: item.shifu.avatar,
                                    type: item.shifu.status,
                                    times: item.shifu.shifu_date.count,
                                    favorableRate: item.shifu.shifu_date.good,
                                    grade: item.shifu.shifu_date.score,
                                    price: item.money,
                                }
                            }
                            list.push(order);
                        });
                    }
                    return resolve(list);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 订单详情
     */
    orderDetail(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    let order = {}, data = response.data;
                    if (options.listType == 't1' || options.listType == 't2') {
                        // 基础信息
                        order = {
                            id: data.id,
                            orderId: data.order,
                            cate: data.cate_name,
                            content: data.desc,
                            createTime: data.time,
                            serviceTime: data.times,
                            orderType: data.types,
                            state: data.status,
                            retainageMoney: data.moneyb,
                            needAppraise: data.message <= 0,
                            worker: {},
                            workerList: [],
                        };
                        if (options.listType == 't1') {
                            order.images = [];
                            data.goods.forEach(item => {
                                order.images.push(item.thumbnail);
                            });
                        }
                        if (options.listType == 't2') {
                            order.images = prototype.$utils.isType(data.img, "array") ? data.img : [];
                        }
                        // 地址
                        order.address = {
                            address: data.address.address,
                            detail: data.address.doorplate,
                            name: data.address.name,
                            mobile: data.address.mobil,
                            gender: data.address.sex == "男" ? 1 : 2,
                            lng: data.address.lng,
                            lat: data.address.lat,
                        }
                        // 师傅
                        if (prototype.$utils.isType(data.shifu, "object")) {
                            order.worker = {
                                id: data.shifu.id,
                                uid: data.shifu.uid,
                                name: data.shifu.name,
                                avatar: data.shifu.avatar,
                                price: data.money,
                            }
                        }
                        // 师傅列表
                        if (options.listType == 't2' && data.shifus.length > 0) {
                            data.shifus.forEach(worker => {
                                order.workerList.push({
                                    id: worker.id,
                                    uid: worker.uid,
                                    name: worker.name,
                                    avatar: worker.avatar,
                                    price: worker.money,
                                    createTime: worker.date,
                                    type: worker.status,
                                    times: worker.data.count,
                                    favorableRate: worker.data.good,
                                    grade: worker.data.score,
                                })
                            });
                        }
                    } else if (options.listType == 't3') {
                        order = {
                            id: data.id,
                            orderId: data.order,
                            createTime: data.times,
                            serviceTime: data.starttime,
                            pickupAddress: {
                                address: data.address_a.address,
                                detail: data.address_a.doorplate,
                                name: data.address_a.name,
                                mobile: data.address_a.mobil,
                                gender: data.address_a.sex == "男" ? 1 : 2,
                                lng: data.address_a.lng,
                                lat: data.address_a.lat,
                            },
                            unloadAddress: {
                                address: data.address_b.address,
                                detail: data.address_b.doorplate,
                                name: data.address_b.name,
                                mobile: data.address_b.mobil,
                                gender: data.address_b.sex == "男" ? 1 : 2,
                                lng: data.address_b.lng,
                                lat: data.address_b.lat,
                            },
                            state: data.status,
                            price: data.money,
                            needAppraise: false,
                            worker: {},
                        };
                        // 师傅
                        if (prototype.$utils.isType(data.worker, "object")) {
                            order.worker = {
                                id: data.worker.id,
                                uid: data.worker.uid,
                                name: data.worker.name,
                                avatar: data.worker.avatar,
                            }
                        }
                    }
                    return resolve(order);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 选择师傅
     */
    chooseWorker(id, workerId) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.chooseWorker",
                data: {
                    id: id,
                    worker: workerId,
                }
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.msg);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 完成订单
     */
    finishOrder(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then((response) => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 取消订单
     */
    cancelOrder(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then((response) => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 退款
     */
    refundOrder(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 评价订单
     */
    appraiseOrder(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 修改服务时间
     */
    changeServiceTime(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
}
